//订单数据库方法
package com.cuijian.demo.model;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

import java.util.Date;
import java.util.List;

public class LogisticsOrder extends Model<LogisticsOrder> {

    public static final LogisticsOrder dao = new LogisticsOrder();

    // 订单状态常量
    public static final int STATUS_PENDING = 0;      // 待发货
    public static final int STATUS_SHIPPING = 1;     // 运输中
    public static final int STATUS_DELIVERING = 2;   // 派送中
    public static final int STATUS_DELIVERED = 3;    // 已签收
    public static final int STATUS_EXCEPTION = 4;    // 异常

    /**
     * 根据用户ID分页查询订单
     */
    public Page<LogisticsOrder> paginateByUserId(int pageNumber, int pageSize, Long userId) {
        return dao.paginate(
                pageNumber,
                pageSize,
                "SELECT lo.*, u.username as user_name",
                "FROM logistics_order lo LEFT JOIN user u ON lo.user_id = u.id WHERE lo.user_id = ? ORDER BY lo.create_time DESC",
                userId
        );
    }

    /**
     * 根据物流单号查询订单
     */
    public LogisticsOrder findByTrackingNumber(String trackingNumber) {
        return dao.findFirst("SELECT lo.*, u.username as user_name FROM logistics_order lo LEFT JOIN user u ON lo.user_id = u.id WHERE lo.tracking_number = ?", trackingNumber);
    }

    /**
     * 查询用户的所有订单
     */
    public List<LogisticsOrder> findByUserId(Long userId) {
        return dao.find("SELECT * FROM logistics_order WHERE user_id = ? ORDER BY create_time DESC", userId);
    }

    /**
     * 根据状态查询用户订单
     */
    public List<LogisticsOrder> findByUserIdAndStatus(Long userId, Integer status) {
        return dao.find("SELECT * FROM logistics_order WHERE user_id = ? AND status = ? ORDER BY create_time DESC", userId, status);
    }

    /**
     * 检查物流单号是否存在
     */
    public boolean isTrackingNumberExists(String trackingNumber) {
        return dao.findFirst("SELECT id FROM logistics_order WHERE tracking_number = ?", trackingNumber) != null;
    }

    /**
     * 更新订单状态
     */
    public boolean updateStatus(Long orderId, Integer status) {
        return dao.findById(orderId)
                .set("status", status)
                .set("update_time", new Date())
                .update();
    }

    /**
     * 标记为已签收
     */
    public boolean markAsDelivered(Long orderId) {
        return dao.findById(orderId)
                .set("status", STATUS_DELIVERED)
                .set("actual_delivery", new Date())
                .set("update_time", new Date())
                .update();
    }

    // Getter方法
    public Long getId() {
        return getLong("id");
    }

    public Long getUserId() {
        return getLong("user_id");
    }

    public String getTrackingNumber() {
        return getStr("tracking_number");
    }

    public String getRecipientName() {
        return getStr("recipient_name");
    }

    public String getRecipientAddress() {
        return getStr("recipient_address");
    }

    public String getRecipientPhone() {
        return getStr("recipient_phone");
    }

    public String getLogisticsCompany() {
        return getStr("logistics_company");
    }

    public Date getOrderTime() {
        return getDate("order_time");
    }

    public Date getEstimatedDelivery() {
        return getDate("estimated_delivery");
    }

    public Date getActualDelivery() {
        return getDate("actual_delivery");
    }

    public Integer getStatus() {
        return getInt("status");
    }

    public String getRemarks() {
        return getStr("remarks");
    }

    public Date getCreateTime() {
        return getDate("create_time");
    }

    public Date getUpdateTime() {
        return getDate("update_time");
    }

    public String getUserName() {
        return getStr("user_name");
    }

    /**
     * 获取状态文本
     */
    public String getStatusText() {
        Integer status = getStatus();
        if (status == null) return "未知";

        switch (status) {
            case STATUS_PENDING: return "待发货";
            case STATUS_SHIPPING: return "运输中";
            case STATUS_DELIVERING: return "派送中";
            case STATUS_DELIVERED: return "已签收";
            case STATUS_EXCEPTION: return "异常";
            default: return "未知";
        }
    }
}